#include"map.h"

//constructor
MAP::MAP(){
	size=15;
	peta=new grid*[15];	
	for (int j=0;j<size;j++){
			peta[j]=new grid[size];
			}	
}

MAP::MAP(int s){
	size=s;
	peta=new grid*[size];	
	for (int j=0;j<size;j++){
			peta[j]=new grid[size];
			}
}

MAP::MAP(const MAP& m){
	size=m.size;
	for (int i=0;i<size;i++){
		for (int j=0;j<size;j++){
			peta[i][j]=m.peta[i][j];
			}	
	}
}

MAP& MAP::operator= (const MAP m)
{	
	size=m.size;
	for (int i=0;i<size;i++){
		for (int j=0;j<size;j++){
			peta[i][j]=m.peta[i][j];
			}	
	}
	return *this;
}

//destructor
MAP::~MAP(){
	size=0;
	delete []peta;
}

//fungsi
int MAP::getSize(){
	return size;
}

void MAP::setSize(int s){
	size=s;
}

grid MAP::getElement(int b,int k){
	return peta[k][b];
}

grid& MAP::editElement(int b, int k){
	return peta[b][k];
}

void MAP::setElement(int b,int k,grid g){
	peta[b][k]=g;
}

void MAP::parseInput(string S){
	ifstream input;
	input.open(S.c_str());
	if(!input){
		cout<<"File open error"<<endl;
	}else{
		char temp;
		int i = 0;
		int j = 0;
		while(!input.eof()){
			input>>temp;
			if(i<size){								
				if(temp=='0'){
					peta[i][j].setTipe(0);
				}else if(temp=='1'){
					peta[i][j].setTipe(1);
				}else if(temp=='2'){
					peta[i][j].setTipe(2);
				}else if(temp=='s'){
					setStartPoint(i,j);
					peta[i][j].setTipe(1);
				}else if(temp=='e'){
					setEndPoint(i,j);
					peta[i][j].setTipe(1);
				}
				cout<<" "<<i<<" "<<j<<" "<<temp<<endl;
				j++;
				if(j>=size){			
					j = 0;
					i++;
				}
			}			
		}
	}
	cout<<"Done parsing"<<endl;
	input.close();	
}

void MAP::genDirection(){
	pair<int,int> startP,endP;
	startP=getStartPoint();
	endP=getEndPoint();
	int x,y,si,lastdir=0;
	si=this->getSize();

	x=startP.first;
	y=startP.second;
	
	while (!((x==endP.first)&&(y==endP.second))){
		bool change=false;
		cout<<x<<" "<<y<<" "<<endl;
		if((x-1>=0)&&(!change)){
			if((peta[x-1][y].getTipe()==1)&&(peta[x-1][y].getDir()==0)){
				peta[x][y].setDir(1);
				x--;
				change=true;
				lastdir=1;
			}
		}
		if((x+1<size)&&(!change)){
			if((peta[x+1][y].getTipe()==1)&&(peta[x+1][y].getDir()==0)){
				peta[x][y].setDir(4);
				x++;
				change=true;
				lastdir=4;
			}
		}
		if((y-1>=0)&&(!change)){
			if((peta[x][y-1].getTipe()==1)&&(peta[x][y-1].getDir()==0)){
				peta[x][y].setDir(3);
				y--;
				change=true;
				lastdir=3;
			}
		}
		if((y+1<size)&&(!change)){
			if((peta[x][y+1].getTipe()==1)&&(peta[x][y+1].getDir()==0)){
				peta[x][y].setDir(2);
				y++;
				change=true;
				lastdir=2;
			}
		}
		change=false;
	}
	peta[x][y].setDir(lastdir);
}

int MAP::getBobot(int x, int y){
	if(peta[x][y].getTipe()!=2){
		return 0;
	}else{
		int total=0;
		if(x-1>=0){
			if(peta[x-1][y].getTipe()==1){total++;}
			if(y-1>=0){
				if(peta[x-1][y-1].getTipe()==1){total++;}
			}
			if(y+1<size){
				if(peta[x-1][y+1].getTipe()==1){total++;}
			}
		}
		if(x+1<size){
			if(peta[x+1][y].getTipe()==1){total++;}
			if(y-1>=0){
				if(peta[x+1][y-1].getTipe()==1){total++;}
			}
			if(y+1<size){
				if(peta[x+1][y+1].getTipe()==1){total++;}
			}
		}
		if(y-1>=0){
			if(peta[x][y-1].getTipe()==1){total++;}
		}
		if(y+1<size){
			if(peta[x][y+1].getTipe()==1){total++;}
		}
		return total;
	}
}

void MAP::setEndPoint(int x,int y){
	endPoint.first=x;
	endPoint.second=y;
}
		
pair<int,int> MAP::getEndPoint(){
	return(make_pair(endPoint.first,endPoint.second));
}

void MAP::setStartPoint(int x,int y){
	startPoint.first=x;
	startPoint.second=y;
	(this->getElement(x,y)).setTipe(1);

}

pair<int,int> MAP:: getStartPoint(){
	return(make_pair(startPoint.first,startPoint.second));
}

pair<int,int> MAP::whichGrid(int x,int y){
	int max_x = 32*size;
	int max_y = 32*size;
	int temp_x = 32;
	int temp_y = 32;
	int i=0;
	int j=0;
	bool found=false;
	while((i<size)&&(!found)){
		if(x<temp_x){
			found = true;
		}else{
			temp_x = temp_x + 32;
			i++;
		}
	}
	found = false;
	while((j<size)&&(!found)){
		if(y<temp_y){
			found = true;
		}else{
			temp_y = temp_y + 32;
			j++;
		}
	}
	if((i<size)&&(j<size)){
		return(make_pair(i,j));
	}else{
		return(make_pair(-1,-1));
	}
}

pair<int,int> MAP::gridPos(int i, int j){
	return(make_pair(i*32,j*32));
}

void MAP::drawMap(sf::RenderWindow& window,sf::Sprite& s1,sf::Sprite& s2,sf::Sprite& s3){
	for(int i=0;i<size;i++){
		for(int j=0;j<size;j++){
			//cout<<peta[i][j].getDir()<<" ";
			if (peta[i][j].getDir() != 0) {
			s2.SetPosition(j*32,i*32);
			window.Draw(s2);
			} else
			{
			s1.SetPosition(j*32,i*32);
			window.Draw(s1);
			}
		}
		//cout<<endl;
	}
	window.Draw(s3);
}
		
